/*
 * Host.cpp
 *
 *  Created on: Jan 26, 2012
 *      Author: jiabin
 */

#include "Host.h"
#include <cstring>
#include <cstdlib>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>

Host::Host(const std::string& ip, int port, bool isUDP)
{
	this->udp = isUDP;
	bzero((void *) &addr, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(port);
	inet_aton(ip.c_str(), &addr.sin_addr);
}

unsigned int Host::convertIP(const std::string& ipstr)
{
	unsigned int result = 0, temp;
	size_t dot1 = ipstr.find('.');
	size_t dot2 = ipstr.find('.', dot1 + 1);
	size_t dot3 = ipstr.find('.', dot2 + 1);

	temp = atoi(ipstr.substr(0, dot1).c_str());
	result += temp * 256 * 256 * 256;

	temp = atoi(ipstr.substr(dot1 + 1, dot2 - dot1).c_str());
	result += temp * 256 * 256;

	temp = atoi(ipstr.substr(dot2 + 1, dot3 - dot2).c_str());
	result += temp * 256;

	temp = atoi(ipstr.substr(dot3 + 1, ipstr.length() - dot3).c_str());
	result += temp;

	return result;
}

